<template>
	<view>
		<uni-nav-bar @clickLeft="handleClick" class="navbar" left-icon="scan" height="120rpx" :border='false' fixed
			title="首页">
		</uni-nav-bar>
		<scroll-view scroll-y="true">
			<view class="container">
				<view>
					<view class="container-title">
						<text class="title-book">图书热榜</text>
						<text></text>
					</view>
					<view class="content-coat">
						<view v-for="(item,index) in hotBookList" :key="item.bookId" class="content-item">
							<image @click="handleJump(item.bookId)" :src="item.url" mode=""></image>
							<p class="book-name">{{item.bookName}}</p>
						</view>
					</view>
				</view>
				<view>
					<view class="container-title">
						<text class="title-book">借阅记录</text>
						<text></text>
					</view>
					<view class="content-coat">
						<view v-for="(item,index) in lendList" :key="item.lendId" class="content-item">
							<image @click="handleJumpLend(item.lendId,item.bookId)" :src="item.url" mode=""></image>
							<p class="book-name">{{item.bookName}}</p>
						</view>
					</view>
				</view>
				<view style="height: 80%;">
					<view class="container-title">
						<text class="title-book">罚款记录</text>
						<text @click="handleFine" class="title-book">查看更多</text>
					</view>
					<view>
						<view class="content-item-penalty">
							<view>
								<view class="content-penalty">
									<uni-table stripe emptyText="暂无更多数据">
										<uni-tr>
											<uni-th width="300rpx" align="center">图书</uni-th>
											<uni-th width="180rpx" align="center">罚款金额</uni-th>
											<uni-th width="auto" align="center">是否赔偿</uni-th>
										</uni-tr>
										<uni-tr v-for="(item,index) in penaltyList" :key="index">
											<uni-td align="center">{{item.name}}</uni-td>
											<uni-td align="center">{{item.penaltyPrice}}元</uni-td>
											<uni-td align="center">未赔偿</uni-td>
										</uni-tr>
									</uni-table>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		getToken
	} from '../utils/auth.js'
	import {
		getHotBookList,
		getLendList,
		getPenaltyList,
		searchBookInfoByISBN
	} from '../api/index/index.js';
	import {
		getBorrowList
	} from '../api/system/borrow.js'
	import {
		getBookInfo
	} from '../api/work/work.js'
	import {
		login
	} from '../utils/dingdingLogin.js'

	export default {
		data() {
			return {
				hotBookList: [],
				lendList: [],
				penaltyList: []
			}
		},
		methods: {
			handleJump(bookId) {
				uni.navigateTo({
					url: `/pages/work/detail/index?bookId=${bookId}`,
				})
			},
			handleJumpLend(lendId, bookId) {
				uni.navigateTo({
					url: "/pages/mine/borrow/detail/index?lendId=" + lendId + "&bookId=" + bookId,
				})
			},
			handleFine() {
				uni.navigateTo({
					url: "/pages/finelist/finelist",
				})
			},
			getBookList() {
				getHotBookList().then(res => {
					this.hotBookList = res.rows
				})
			},
			getLendList() {
				getLendList({
					'isBack': 0,
					"userId": this.$store.getters.id,
				}).then(res => {
					this.lendList = res.rows
				})
			},
			getPenaltyList() {
				getPenaltyList({
					'isIndemnify': 0
				}).then(res => {
					this.penaltyList = res.rows
				})
			},
			handleClick() {
				uni.scanCode({
					scanType: ["barCode", "qrCode"],
					onlyFromCamera: true,
					success: (res) => {
						if (res.scanType === 'EAN_13')
							getBookInfo({
								// searchByName: 'res.result',
								searchByName: "9787500877356",
								pageSize: 5,
								pageNum: 1
							}).then(resp => {
								console.log(resp);
								if (resp.rows != null) {
									// 数据库中有该图书
									let book = resp.rows[0]
									getBorrowList({
										bookId: book.bookId,
										userId: this.$store.getters.id,
										isBack: 0 // 未归还
									}).then(resp0 => {
										// 有借阅记录
										if (resp0.total != 0) {
											uni.navigateTo({
												url: "/pages/mine/borrow/detail/index?lendId=" +
													resp0.rows[0].lendId +
													"&bookId=" + resp0.rows[0].bookId,
											})
										} else {
											uni.navigateTo({
												url: '/pages/work/detail/index?bookId=' +
													book.bookId
											})
										}
									})
								}
								// ' else {
								// 	addBookByISBN(res.result).then(resp => {
								// 		console.log("录入成功");
								// 	})
								// }'
							})
					}
				})
			},
			getInfo() {
				this.$store.dispatch('GetInfo')
			}
		},
		onLoad() {
			this.$store.dispatch('Login').then(() => {
				this.getInfo()
			})
			this.getBookList()
			this.getLendList()
			this.getPenaltyList()
		}
	}
</script>

<style lang="scss">
	/deep/ .uni-navbar__header {
		padding-top: 40rpx !important;
	}

	.title-book {
		font-size: 16px;
		margin: 10px 0;
	}

	.container {
		display: flex;
		flex-direction: column;

		.content-item-penalty {
			font-size: 14px
		}

		.content-coat {
			overflow-x: scroll;
			display: flex;
			height: 85%;
			flex-direction: row;

			.content-item {
				padding: 10px;
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 33%;
				height: 100%;

				image {
					height: 100%;
				}

				.book-name {
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					width: 120%;
					text-align: center;
				}
			}
		}

	}

	.container>view {
		width: 100%;
		height: 200px;
		border: 1px solid #ccc;
		padding: 10px;
	}

	.container>view:nth-child(3) {
		height: 100%;
	}

	.container>view .container-title {
		font-size: 20px;
		margin-bottom: 5px;
	}

	.container>view .container-title text:nth-child(2) {
		float: right;
	}
</style>